/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.utils;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Looper;
import android.util.Log;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import org.me.schedule.Calendar;
import org.me.schedule.Day;
import org.me.schedule.Month;
import org.me.ttvdroid.TotalView;

/**
 *
 * @author peterholtegaard@gmail.com
 */
public class CalendarProvider {
    private ContentResolver cr;
    private Calendar totalViewCalendar;//the calendar from totaltView
    private Calendar phoneCalendar;//the phones current calendar
    private Calendar editableCalendar;//a temp calendar instance, for editing.
    GregorianCalendar today = new GregorianCalendar();
    

    private static CalendarProvider instance;

    private CalendarProvider(ContentResolver cr)
    {
        this.cr = cr;
        

    }

    public static CalendarProvider getInstance(ContentResolver cr)
    {
        if(instance == null)instance = new CalendarProvider(cr);
      
        return instance;

    }

    public ArrayList<CharSequence> getPhoneCalendars()
    {
        ArrayList<CharSequence> calendars = new ArrayList<CharSequence>();
        Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{"_id", "displayname"}, null, null, null);
            cursor.moveToFirst();
            String[] CalNames = new String[cursor.getCount()];
            int[] CalIds = new int[cursor.getCount()];
            for (int i = 0; i < CalNames.length; i++) {
                int calId = cursor.getInt(0);
                String calName = cursor.getString(1);
                calendars.add(calName);
                CalIds[i] = calId;
                CalNames[i] = calName;
                Log.e("kalender id: " +calId, "kalendernavn: " +calName);
                cursor.moveToNext();
            }

            cursor.close();

            return calendars;
    }

    
    public Month getTTVCalendar(String userName, String password, String month)
    {
        Month tMonth = null;
        /*if(totalViewCalendar == null)
        {
            NetworkDataProvider comm = NetworkDataProvider.getInstance();
            comm.logIn(userName, password);
            tMonth = Month.getMonthFromTTVHtml(comm.getMonthAsHtml(month, 2010),Integer.parseInt(month));
            totalViewCalendar = new Calendar();
        }
        return tMonth;*/
         Looper.prepare();
        //tMonth = new CalendarReader(userName, password, month).getTTVCalendar();
        return tMonth;
        
    }

    public void addToCalendar(Month month, Long calId)
    {
        Uri eventsUri = Uri.parse("content://calendar/events");
        ArrayList<Day> days = month.getDays();
         for (Day day : days) {
            try {
                ContentValues event = day.toEvent();
                event.put("calendar_id", calId + 1);
                Uri url = cr.insert(eventsUri, event);
                Log.e("kalenderfejl", "næh");

            } catch (Exception e) {
                Log.e("kalenderfejl", e.getMessage());
            }
        }

    }

    /*public Calendar getTotalViewCalendar()
    {
    Day[] cal = StuffParser.parseToDays(Syncronizer.getInstance().getVagtPlan());
    return null;
    }*/


}
